//
//  ChooseProCell.swift
//  SlaughterClient
//
//  Created by 邱仙凯 on 2021/9/15.
//  Copyright © 2021 zwb. All rights reserved.
//

import UIKit

class ZyaChooseProCell: UITableViewCell {
    var markClosure:(()->())?
 
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
         selectionStyle = .none
        
        addSubview(content)
        addSubview(leftButton)
        leftButton.addTarget(self, action: #selector(markAction), for: .touchUpInside)
        rightButton.isUserInteractionEnabled = false

       
        addSubview(rightButton)
        
        setupConstraints()
        
    }
    //父级别，存在子节点
    func setSuperType(with name: String) {
        content.frame = CGRect(origin: .zero, size: CGSize(width: ks_width - 2*15, height: 10))
        content.text = name
        content.sizeToFit()

        leftButton.isHidden = true
        rightButton.isHidden = true
        self.accessoryType = .disclosureIndicator
                
        content.snp.remakeConstraints { make in
            make.left.equalToSuperview().offset(15)
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-15)
            make.height.equalTo(content.ks_height)
        }
        
    }
    
    // 最终节点形态
    func setLastNodeType(with model:ZyaFOGoodTModel) {
        leftButton.isHidden = false
        rightButton.isHidden = false
        self.accessoryType = .none
        
//        if TenderAppear.shared.projectType == .awClient {
            content.text = model.sAppGoodsNm.unwrap
//        } else {
//            content.text = model.sName.unwrap
//        }
        content.frame = CGRect(origin: .zero, size: CGSize(width: ks_width - 40 - 37 - 20, height: 10))
        content.sizeToFit()
        content.snp.remakeConstraints { make in
            make.left.equalToSuperview().offset(50)
            make.right.equalToSuperview().offset(-47)
            make.centerY.equalToSuperview()
            make.height.equalTo(content.ks_height)
        }
        
        
        self.setMarkStatus(flag: model.sMark == "1")
        self.setCheckStatus(flag: model.selectX)
    }
    
    
    private func setMarkStatus(flag:Bool) {
        if flag {
            leftButton.setImage(UIImage(named: "shoucang_red"), for: UIControl.State())
        } else {
            leftButton.setImage(UIImage(named: "shoucang"), for: UIControl.State())

        }
    }
    private func setCheckStatus(flag:Bool) {
        if flag {
            rightButton.setImage(UIImage(named: "s_select_red"), for: UIControl.State())
        } else {
            rightButton.setImage(UIImage(named: "s_yuan"), for: UIControl.State())

        }
    }
   
    @objc func markAction() {
        if let clo = markClosure {
            clo()
        }
      }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    private func setupConstraints () {
        
        leftButton.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(10)
            make.width.equalTo(30)
            make.height.equalTo(30)
            make.centerY.equalToSuperview()
        }
        
        rightButton.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-10)
            make.width.equalTo(27)
            make.height.equalTo(27)
            make.centerY.equalToSuperview()
        }
        
        content.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(15)
            make.centerY.equalToSuperview()
        }
        
        
    }
//    var one:ZyaFOGoodType? {
//        didSet {
//            if let one = one {
//                content.text = one.s_type_name.unwrap
//                self.setSuperType()
//            }
//        }
//    }
//    var oneSec:ZyaFOGoodSecType? {
//        didSet {
//            if let one = oneSec {
//                content.text = one.s_sec_type_name.unwrap
//                self.setSuperType()
//            }
//        }
//    }
//    var oneThird:ZyaFOGoodTModel? {
//        didSet {
//            if let one = oneThird {
//                setLastNodeType()
//                content.text = "        " + one.sName.unwrap
//                self.setMarkStatus(flag: one.sMark == "1")
//                self.setCheckStatus(flag: one.selectX)
//            }
//        }
//    }
//    
    
    lazy var leftButton: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "shoucang"), for: UIControl.State())
        return btn
    } ()
    
    lazy var content: UILabel = {
        let label = UILabel()
        label.text = "--"
        label.textColor = Theme.textColor
        label.font = UIFont.systemFont(ofSize: 15)
//        if TenderAppear.shared.projectType == .awClient {
            label.numberOfLines = 0
//        }
        return label
    } ()
    
    lazy var rightButton: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named:"s_yuan"), for: UIControl.State())
        return btn
    } ()
    
    
}
